package com.github.livebase.util.template;

import android.util.ArrayMap;

import java.lang.ref.WeakReference;
import java.util.Map;

/**
 * @ClassName Template
 * @description:
 * @author: biao532
 * @create: 2023-12-27 23:20
 * @Version 1.0
 **/
public class Template extends TemplateAwareExpressionParser{
    public static final Template template = new Template();
    private WeakReference<Map<String, IExpression>> cache = new WeakReference<>(null);
    @Override
    protected IExpression doParseExpression(String expressionString) throws ParseException {
        Map<String, IExpression> c = cache.get();
        if (c == null) {
            c = new ArrayMap<>();
            cache = new WeakReference<>(c);
        }
        IExpression expression = c.get(expressionString);
        if (expression == null) {
            expression = new VarExpression(expressionString);
            c.put(expressionString, expression);
        }
        return expression;
    }

    @Override
    protected boolean isTemplate() {
        return true;
    }

    @Override
    protected String getExpressionPrefix() {
        return "{";
    }

    @Override
    protected String getExpressionSuffix() {
        return "}";
    }
}
